package com.weishi.project.sport.home.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.weishi.common.utils.StringUtils;
import com.weishi.framework.web.controller.BaseController;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.project.api.vo.LoginVo;
import com.weishi.project.sport.home.service.SportLoginService;
import com.weishi.project.training.domain.Dynamometer;
import com.weishi.project.training.domain.Sportsman;
import com.weishi.project.training.service.IDynamometerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "运动员端--登录", description = "运动员端--登录")
@RestController
@CrossOrigin("*")//解决移动端跨域
@RequestMapping("/sport/")
public class SportLoginController extends BaseController {

    /**
     * 运动员登录接口
     */
    @Autowired
    private SportLoginService sportLoginService;

    /**
     * 测功仪历史数据，接口
     */
    @Autowired
    private IDynamometerService dynamometerService;


    @ApiOperation("运动员端 登录")
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult sportLogin(LoginVo login)
    {
        return sportLoginService.sportLogin(login);
    }

    @RequestMapping(value = "info", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult sportLogin()
    {
        return AjaxResult.success(currentLoginSportsman());
    }


    @ApiOperation("运动员端 退出登录")
    @RequestMapping(value = "sportLogout", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult sportLogout(HttpServletRequest request)
    {
        return sportLoginService.sportLogout(request);
    }


    @ApiOperation("运动员端 注册")
    @RequestMapping(value = "register", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult register(Sportsman sportsman)
    {
        return sportLoginService.register(sportsman);
    }


    @ApiOperation("运动员端 修改密码")
    @RequestMapping(value = "updatePassword", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updatePassword(@RequestBody JSONObject jsonObject)
    {
        return sportLoginService.updatePassword(currentLoginSportsman().getId() , jsonObject);
    }


    @Transactional
    @ApiOperation("手动汇总测功仪历史数据合并")
    @RequestMapping(value = "dataMerging", method = RequestMethod.POST)
    public AjaxResult dataMerging()
    {
        List<Map<String,Object>> jsondate = new ArrayList<>();
        Dynamometer dynamometer = new Dynamometer();
        List<Dynamometer> dynamometerList = dynamometerService.list(Wrappers.<Dynamometer>lambdaQuery()
                .eq(Dynamometer::getTrainingDayId , 2185)
                .eq(Dynamometer::getGroupId , 2504)
                .eq(Dynamometer::getSportsmanId , 204));
        // 更新测功仪历史数据
        if(dynamometerList.size() > 0)dynamometerList.forEach( p-> {
            StringUtils.copyProperties(p , dynamometer);
            Map<String,Object> temp = new HashMap<>();
            temp.put("mileage" , dynamometer.getMileage());
            temp.put("propellerFrequency" , dynamometer.getPropellerFrequency());
            temp.put("heartRate" , dynamometer.getHeartRate() == null ? 0 : dynamometer.getHeartRate());
            temp.put("lengthTime" , dynamometer.getLengthTime());
            temp.put("speed" , dynamometer.getSpeed() == null ? 0.0 : dynamometer.getSpeed());
            temp.put("power" , dynamometer.getPower());
            temp.put("watt" , dynamometer.getWatt());
            temp.put("peakForce" , dynamometer.getPeakForce() == null ? 0.0 : dynamometer.getPeakForce());
            temp.put("averageForce" , dynamometer.getAverageForce() == null ? 0.0 : dynamometer.getAverageForce());
            temp.put("pulpNumber" , dynamometer.getPulpNumber() == null ? 0 : dynamometer.getPulpNumber());
            temp.put("trainingMode" , dynamometer.getTrainingMode() == null ? 0.0 : dynamometer.getTrainingMode());
            temp.put("segmentNumber" , dynamometer.getSegmentNumber() == null ? 0.0 : dynamometer.getSegmentNumber());
            jsondate.add(temp);
        });
        // 更新最后一条数据
        Dynamometer dynamometer1 = dynamometerService.getById(90681);
        dynamometer1.setJsonData(JSONArray.toJSONString(jsondate));
        dynamometer1.setId(90681);
        dynamometerService.updateById(dynamometer1);
        return AjaxResult.success();
    }






}
